﻿using IDAdmin.Common;
using IDAdmin.Lib.DataLayer;
using IDAdmin.Lib.UI;
using IDAdmin.Lib.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IDAdmin.Pages
{
    public partial class GetLauList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private Table getLau(List<Au> auData)
        {
            List<Au> auList = new List<Au>(auData.Count);

            auData.ForEach((item) =>
            {
                auList.Add(new Au(item));
            });

            foreach (Au auItem in auList)
            {
                auItem.Timestamp = ConverterWorkData.truncateTime(auItem.Timestamp, "dd/MM/yyyy");
            }

            List<string> timeList = new List<string>();
            DateTime dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            foreach (DateTime item in dateTimeList)
            {
                string timestamp = ConverterWorkData.truncateTime(ConverterWorkData.kindDateTimeToKindUnixTimestamp(item, DateTimeKind.Local).ToString(), "dd/MM/yyyy");
                timeList.Add(timestamp);
            }
            timeList = timeList.OrderByDescending(o => o).ToList();

         

            List<LoginData> loginDataList = new List<LoginData>();
            
            var lauList = new[] { new { Account = "", Date = "" } }.ToList();
            lauList.RemoveAt(0);
            for (int i = 0; i < timeList.Count; i++)
            {
                
                LoginData ld = new LoginData();

                List<string> nauList = new List<string>();

                #region accountAtTimeList
                //var accountAtTimeList = (from tb in auList where tb.Timestamp.Equals(timeList[i]) select new { tb.Account }).Distinct().ToList();
                var accountAtTimeList = new[] { new { Account = "" } }.ToList();
                accountAtTimeList.RemoveAt(0);
                foreach (var item in auList)
                {
                    if (item.Timestamp.Equals(timeList[i]))
                    {
                        accountAtTimeList.Add(new { Account = item.Account });
                    }
                }
                accountAtTimeList = accountAtTimeList.Distinct().ToList();

                #endregion

                //var dauList = (from tb2 in (from tb in auList.AsParallel() group tb by new { tb.Account, tb.Timestamp } into grp select new { grp.Key.Account, grp.Key.Timestamp }).AsParallel() group tb2 by new { tb2.Timestamp } into grp2 select new { grp2.Key.Timestamp, Count = grp2.Count() });
                //var accountAtTimelist = (from tb in auList.AsParallel() where tb.Timestamp.Equals(timeList[i]) select new { tb.Account }).Distinct().ToList();
                var checkDayNum = (from tb in timeList.AsParallel() where tb.CompareTo(timeList[i]) > 0 select tb).Count().ToString();
                if (int.Parse(checkDayNum) >= 7)
                {
                    DateTime dtTemp = ConverterWorkData.kindUnixTimeStampToKindDateTime(double.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local);
                    List<DateTime> sevenDayAfterList = new List<DateTime>();
                    //sevenDayBeforeList.Add(dtTemp);
                    for (int j = 1; j <= 7; j++)
                    {
                        sevenDayAfterList.Add(dtTemp.AddDays(j));
                    }

                    int count = 0;
                    foreach (var item in accountAtTimeList)
                    {
                        int numLoop = 0;
                        foreach (DateTime dtItem in sevenDayAfterList)
                        {
                            string dtCurrent = ConverterWorkData.truncateTime(ConverterWorkData.kindDateTimeToKindUnixTimestamp(dtItem, DateTimeKind.Local).ToString(), "dd/MM/yyyy");
                            var hasLogin = (from tb in auList.AsParallel() where tb.Account.Equals(item.Account) && tb.Timestamp.Equals(dtCurrent) select tb).Count().ToString();
                            if (int.Parse(hasLogin) <= 0)
                            {
                                numLoop++;
                            }
                        }
                        if (numLoop.Equals(7))
                        {
                            count++;
                            lauList.Add(new { Account = item.Account, Date = ConverterWorkData.kindUnixTimeStampToKindDateTime(long.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local).ToString("dd/MM/yyyy") });
                        }
                    }
                }
            }

            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Date",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Account",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader")
                }
            );

            table.Rows.Add(rowHeader);
            try
            {
                if (lauList == null || lauList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    foreach (var dr in lauList)
                    {
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Date),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Account),HorizontalAlign.Center,css),
                            }
                        );

                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }
        protected void getLauButton_Click(object sender, EventArgs e)
        {
            DateTime dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            dataPanel.Controls.Clear();
            dataPanel.Controls.Add(getLau(WebDB.InsertAUDataToSqlserver(dateTimeList)));
        }
    }
}